using micromsg;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Network;
using MicroMsg.Protocol;
using MicroMsg.Storage;
using System;
namespace MicroMsg.Scene
{
	public class NetSceneReg : NetSceneBaseEx<RegRequest, RegResponse, RegRequest.Builder>
	{
		private const string TAG = "NetSceneReg";
		public void doScene(string account, string rawPsw, string bindEmail)
		{
			this.doScene(account, rawPsw, "", 0, bindEmail, "", "", "");
		}
		public void doScene(string account, string rawPsw, string nickName, int bindUin, string bindEmail, string bindMobile, string imgSid, string imgCode)
		{
			this.doScene(account, Util.getCutPasswordMD5(rawPsw), Util.getFullPasswordMD5(rawPsw), nickName, bindUin, bindEmail, bindMobile, imgSid, imgCode);
		}
		private void doScene(string account, string password, string password2, string nickName, int bindUin, string bindEmail, string bindMobile, string imgSid, string imgCode)
		{
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(31);
			this.mBuilder.UserName = Util.toSKString(account);
			this.mBuilder.Pwd = Util.toSKString(password);
			this.mBuilder.NickName = Util.toSKString(nickName);
			this.mBuilder.BindUin = (uint)bindUin;
			this.mBuilder.BindEmail = Util.toSKString(bindEmail);
			this.mBuilder.BindMobile = Util.toSKString(bindMobile);
			this.mBuilder.Ticket = Util.toSKString("");
			this.mBuilder.ImgSid = Util.toSKString(imgSid);
			this.mBuilder.ImgCode = Util.toSKString(imgCode);
			this.mBuilder.Pwd2 = password2;
			this.mBuilder.Language = AccountMgr.GetCurrentLanguage();
			this.mBuilder.DLSrc = (uint)AppInfoHelper.ChannelId;
			this.mSessionPack.mCmdID = 31;
			base.endBuilder();
		}
		protected override void onSuccess(RegRequest request, RegResponse response)
		{
			RetConst ret = (RetConst)response.BaseResponse.Ret;
			if (ret == RetConst.MM_OK)
			{
				Log.smethod_5("NetSceneReg", string.Concat(new object[]
				{
					"success. account=",
					request.UserName,
					" mail=",
					request.BindEmail
				}));
				AccountMgr.onLogin(request.UserName.String);
				HostService.updateAuthBuiltinIP(response.BuiltinIPList);
				this.updateUserInfo(request, response);
				EventCenter.postEvent(EventConst.ON_NETSCENE_MAILREG_SUCCESS, null, null);
				EventCenter.postEvent(EventConst.ON_NETSCENE_AUTH_SUCCESS, null, null);
				return;
			}
			Log.smethod_5("NetSceneReg", "failed.");
			EventCenter.postEvent(EventConst.ON_NETSCENE_MAILREG_ERR, ret, null);
		}
		protected override void onFailed(RegRequest request, RegResponse response)
		{
			EventCenter.postEvent(EventConst.ON_NETSCENE_MAILREG_ERR, -800000, null);
		}
		private void updateUserInfo(RegRequest request, RegResponse response)
		{
			Account curAccount = AccountMgr.getCurAccount();
			curAccount.bytesSessionkey = response.SessionKey.ToByteArray();
			curAccount.bytesServerID = SessionPackMgr.getSeverID();
			curAccount.strPwd = request.Pwd.String;
			curAccount.nUin = response.Uin;
			curAccount.strUsrName = request.UserName.String;
			curAccount.strBindEmail = request.BindEmail.String;
			curAccount.strBindMobile = request.BindMobile.String;
			curAccount.nBindQQ = request.BindUin;
			curAccount.strOfficalNickName = response.OfficialNickName.String;
			curAccount.strOfficalUserName = response.OfficialUserName.String;
			curAccount.nPushMailStatus = (int)response.PushMailStatus;
			curAccount.strQQMicroBlog = response.QQMicroBlogUserName.String;
			curAccount.nNewUser = 1;
			curAccount.dbLastSessionKeyTimeStamp = Util.getNowSeconds();
			if (!AccountMgr.updateAccount())
			{
				Log.smethod_1("NetSceneReg", "add account failed");
			}
		}
		private static void onMailAuthRegHandler(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_MAILREG_SUCCESS)
			{
				Log.smethod_5("MAILREG", "(1/2)onMailReg: need Fill NickName");
				Log.smethod_5("MAILREG", "(2/2)onMailReg Success");
				return;
			}
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_MAILREG_ERR)
			{
				Log.smethod_5("MAILREG", "(2/2)onMailReg error, ret = ");
			}
		}
		public static void testRegisterWithEmail()
		{
			EventWatcher eventWatcher = new EventWatcher(null, null, new EventHandlerDelegate(NetSceneReg.onMailAuthRegHandler));
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_MAILREG_SUCCESS, eventWatcher);
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_MAILREG_ERR, eventWatcher);
			ServiceCenter.sceneReg.doScene("test", "test", "2291270826@qq.com");
		}
		private static void onQQAuthRegHandler(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_AUTH_NEEDREG)
			{
				Log.smethod_5("QQREG", "(1/3)onQQAuthNeedFillUserName");
				ServiceCenter.sceneGetUserName.doScene("halenhuang");
				return;
			}
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_FILLUSERNAME_SUCCESS)
			{
				Log.smethod_5("QQREG", "(2/3)onQQAuthNeedReg");
				ServiceCenter.sceneNewReg.doScene();
				return;
			}
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_NEWREG_SUCCESS)
			{
				Log.smethod_5("QQREG", "(3/3)onQQAuthSuccess");
			}
		}
		public static void testRegisterWithQQ()
		{
			EventWatcher eventWatcher = new EventWatcher(null, null, new EventHandlerDelegate(NetSceneReg.onQQAuthRegHandler));
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_AUTH_NEEDREG, eventWatcher);
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_FILLUSERNAME_SUCCESS, eventWatcher);
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_NEWREG_SUCCESS, eventWatcher);
			ServiceCenter.sceneAuth.doScene("7257036901", "11111111");
		}
		private static void onMobileRegHandler(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_MOBILEREG_SETPHONE_SUCCESS)
			{
				Log.smethod_5("QQREG", "(1/3)onMobileRegHandler");
				string verifyCode = "3535";
				ServiceCenter.sceneBindOpMobileForReg.doSceneVerify(verifyCode);
				return;
			}
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_MOBILEREG_VERIFY_SUCCESS)
			{
				Log.smethod_5("QQREG", "(2/3)onMobileRegHandler");
				ServiceCenter.sceneNewReg.doSceneSetPwdforMobileReg("mm@1234", "halen");
				return;
			}
			if (evtArgs.mEventID == EventConst.ON_NETSCENE_MOBILEREG_SUCCESS)
			{
				Log.smethod_5("QQREG", "(3/3)onMobileRegHandler");
			}
		}
		public static void testRegisterWithMobile()
		{
			EventWatcher eventWatcher = new EventWatcher(null, null, new EventHandlerDelegate(NetSceneReg.onMobileRegHandler));
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_MOBILEREG_SETPHONE_SUCCESS, eventWatcher);
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_MOBILEREG_VERIFY_SUCCESS, eventWatcher);
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_MOBILEREG_SUCCESS, eventWatcher);
			ServiceCenter.sceneBindOpMobileForReg.doScene("18923885064");
		}
	}
}
